package io.finer.erp.jeecg.bas.service.impl;

import io.finer.erp.jeecg.bas.entity.BasCategoryLabel;
import io.finer.erp.jeecg.bas.mapper.BasCategoryLabelMapper;
import io.finer.erp.jeecg.bas.service.IBasCategoryLabelService;
import io.finer.erp.jeecg.bas.vo.BasCategoryLabelVO;
import io.finer.erp.jeecg.bas.vo.CategoryLabelVO;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.hutool.core.collection.CollUtil;

/**
 * @Description: 分类客户标签表
 * @Author: jeecg-boot
 * @Date:   2021-08-11
 * @Version: V1.0
 */
@Service
public class BasCategoryLabelServiceImpl extends ServiceImpl<BasCategoryLabelMapper, BasCategoryLabel> implements IBasCategoryLabelService {
	@Override
	public List<BasCategoryLabel> listByCategory(String categoryId) {
		List<BasCategoryLabel> list = this.lambdaQuery().eq(BasCategoryLabel::getCategoryId, categoryId).orderByAsc(BasCategoryLabel::getCategoryLabel).list();
		return list;
	}

	@Override
	@Transactional 
	public Result<?> editCategoryLabel(CategoryLabelVO labelVO) throws JeecgBootException{
		String categoryId = labelVO.getCategoryId();
		if(StringUtils.isBlank(categoryId)) {
			return Result.error("未传入分类ID");
		}
		List<BasCategoryLabelVO> labelList = labelVO.getLabelList();
		if(CollUtil.isEmpty(labelList)) {
			return Result.error("请传入标签信息");
		}
		List<BasCategoryLabel> cateLabelList = this.listByCategory(categoryId);
		Map<String, BasCategoryLabel> labelMap = Maps.newHashMap();
		if(CollUtil.isNotEmpty(cateLabelList)) {
			for (BasCategoryLabel label : cateLabelList) {
				labelMap.put(label.getId(), label);
			}
		}
		List<BasCategoryLabelVO> insertList = Lists.newArrayList();
		List<BasCategoryLabelVO> updateList = Lists.newArrayList();
		List<String> deleteList = Lists.newArrayList();
		List<String> voIds = Lists.newArrayList();
		for(BasCategoryLabelVO vo : labelList) {
			String voId = vo.getId();
			BasCategoryLabel basCategoryLabel = labelMap.get(voId);
			if(basCategoryLabel == null) {
				insertList.add(vo);
			} else {
				updateList.add(vo);
			}
			voIds.add(voId);
		}
		if(CollUtil.isNotEmpty(cateLabelList)) {
			for (BasCategoryLabel label : cateLabelList) {
				String id = label.getId();
				if(!voIds.contains(id)) {
					deleteList.add(id);
				}
			}
		}
		
		if(CollUtil.isNotEmpty(deleteList)) {
			this.removeByIds(deleteList);
		}
		if(CollUtil.isNotEmpty(insertList)) {
			List<BasCategoryLabel> list = Lists.newArrayList();
			for (BasCategoryLabelVO vo : insertList) {
				BasCategoryLabel label = new BasCategoryLabel();
				label.setCategoryLabel(vo.getCategoryLabel());
				label.setCategoryId(categoryId);
				list.add(label);
			}
			this.saveBatch(list);
		}
		if(CollUtil.isNotEmpty(updateList)) {
			List<BasCategoryLabel> list = Lists.newArrayList();
			for (BasCategoryLabelVO vo : updateList) {
				BasCategoryLabel label = labelMap.get(vo.getId());
				label.setCategoryLabel(vo.getCategoryLabel());
				list.add(label);
			}
			this.updateBatchById(list);
		}
		return Result.ok("保存成功");
	}
}
